package com.jack.bookshop.dao;
import com.jack.bookshop.util.DatabaseUtil;
import com.jack.bookshop.util.PasswordUtil;
import com.jack.bookshop.vo.ResultVo;
import com.jack.bookshop.vo.UserVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    public ResultVo signup(String username, String password) {
        ResultVo result = new ResultVo();
        List<String> list = new ArrayList<String>();
        int i = 0;
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DatabaseUtil.getConnection();
            // 1. 先查看用户名是否存在
            String sql = "select id from user where username = ? ";
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            rs = pst.executeQuery();
            if(rs.next()) {
                i = -1;
            }else {
                rs.close();
                pst.close();
                // 2. 再插入新用户
                String sql2 = "insert into user(username,password) values(?,?) ";
                pst = conn.prepareStatement(sql2);
                pst.setString(1, username);
                pst.setString(2, PasswordUtil.encryptBySHA(password)); // 对密码SHA加密
                i = pst.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.closeAll(rs, pst, conn);
        }
        if(i==1) {
            result.success();
            list.add("用户 " + username + " 注册成功 ！");
        }else if(i==-1) {
            result.fail();
            list.add("该用户名 " + username + " 已存在 ！ 请选择其他用户名。");
        }else {
            result.fail();
            list.add("系统原因，用户注册失败，请重试！");
        }
        result.setList(list);
        return result;
    }

    // 用户登录
    public UserVo signin(String username, String password) {
        UserVo userVo = new UserVo();
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DatabaseUtil.getConnection();
            // 用户名和密码是否匹配
            String sql = "select * from user where username = ? and password = ? ";
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, PasswordUtil.encryptBySHA(password));
            rs = pst.executeQuery();
            if (rs.next()) {
                userVo.setId(rs.getInt("id"));
                userVo.setUsername(username);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.closeAll(rs, pst, conn);
        }
        return userVo;
    }

    public UserVo changepw(String username, String password) {
        UserVo userVo = new UserVo();
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DatabaseUtil.getConnection();
            // 修改密码
            String sql = "update user set password = ? where username = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1, PasswordUtil.encryptBySHA(password));
            pst.setString(2, username);
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.closeAll(rs, pst, conn);
        }
        return userVo;
    }
}
